package com.push.web.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.push.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.push.common.annotation.Log;
import com.push.common.core.controller.BaseController;
import com.push.common.core.domain.AjaxResult;
import com.push.common.enums.BusinessType;
import com.push.web.domain.BizCollection;
import com.push.web.service.IBizCollectionService;
import com.push.common.utils.poi.ExcelUtil;
import com.push.common.core.page.TableDataInfo;

/**
 * 我的收藏Controller
 *
 * @author push
 * @date 2023-02-17
 */
@RestController
@RequestMapping("/information/collection")
public class BizCollectionController extends BaseController
{
    @Autowired
    private IBizCollectionService bizCollectionService;

    /**
     * 查询我的收藏列表
     */
    @PreAuthorize("@ss.hasPermi('information:collection:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizCollection bizCollection)
    {
        startPage();
        List<BizCollection> list = bizCollectionService.selectBizCollectionList(bizCollection);
        return getDataTable(list);
    }

    /**
     * 导出我的收藏列表
     */
    @PreAuthorize("@ss.hasPermi('information:collection:export')")
    @Log(title = "我的收藏", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BizCollection bizCollection)
    {
        List<BizCollection> list = bizCollectionService.selectBizCollectionList(bizCollection);
        ExcelUtil<BizCollection> util = new ExcelUtil<BizCollection>(BizCollection.class);
        return util.exportExcel(list, "我的收藏数据");
    }

    /**
     * 获取我的收藏详细信息
     */
    @PreAuthorize("@ss.hasPermi('information:collection:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bizCollectionService.selectBizCollectionById(id));
    }

    /**
     * 新增我的收藏
     */
    @PreAuthorize("@ss.hasPermi('information:collection:add')")
    @Log(title = "我的收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizCollection bizCollection)
    {
        bizCollection.setUserId(getUserId());
        return toAjax(bizCollectionService.insertBizCollection(bizCollection));
    }

    /**
     * 修改我的收藏
     */
    @PreAuthorize("@ss.hasPermi('information:collection:edit')")
    @Log(title = "我的收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizCollection bizCollection)
    {


        List<Long> aids=Arrays.stream(bizCollection.getArticleIds().split(",")).map(s->Long.parseLong(s.trim())).collect(Collectors.toList());
        List<Long> newAids = new ArrayList<>();
        newAids.addAll(aids);

        BizCollection bizCollectionDb = bizCollectionService.selectBizCollectionById(bizCollection.getId());
        if (StringUtils.isNotEmpty(bizCollectionDb.getArticleIds())){
            List<Long> oldIds= Arrays.stream(bizCollectionDb.getArticleIds().split(",")).map(s->Long.parseLong(s.trim())).collect(Collectors.toList());
            newAids.addAll(oldIds);
        }

        newAids = newAids.stream().distinct().collect(Collectors.toList());
        bizCollection.setArticleIds(newAids.stream().map(Long -> Long.toString()).collect(Collectors.joining(",")));
        return toAjax(bizCollectionService.updateBizCollection(bizCollection));
    }

    /**
     * 删除我的收藏
     */
    @PreAuthorize("@ss.hasPermi('information:collection:remove')")
    @Log(title = "我的收藏", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bizCollectionService.deleteBizCollectionByIds(ids));
    }

    /**
     * 获取我的收藏详细信息
     */
    @PreAuthorize("@ss.hasPermi('information:collection:query')")
    @GetMapping(value = "/my")
    public AjaxResult getInfo()
    {
        if (null == bizCollectionService.selectBizCollectionByUserId(getUserId())){
            BizCollection bizCollection = new BizCollection();
            bizCollection.setUserId(getUserId());
            bizCollectionService.insertBizCollection(bizCollection);
        }
        return AjaxResult.success(bizCollectionService.selectBizCollectionByUserId(getUserId()));
    }
}
